﻿using System;
using System.ComponentModel;
using CRMS.Models;
using System.Windows.Forms;
using System.Linq;


namespace CRMS.Views.ClassroomMgnt
{
    public partial class frmTcUseClassroomInfo : Form
    {
        
        private ClassRoomRep cr = new ClassRoomRep();
       
        public frmTcUseClassroomInfo(int cid,string tname)
        {
            InitializeComponent();
            this.cid = cid;
            this.tName = tname;
            this.position = 0;
        }
        private void promot()
        {
            MessageBox.Show("抱歉，未搜索到您要查询的信息！");
        }

        private void TstripBtnQuery_Click(object sender, EventArgs e)
        {

        }

        private void cboxQueryStyle_SelectedValueChanged(object sender, EventArgs e)
        {
            if (this.cboxQueryStyle.Text == "关键词搜索")
            {
                this.lbBuildingName.Visible = false;
                this.cboxBuildingName.Visible = false;
            }
            else
            {
                this.lbBuildingName.Visible = true;
                this.cboxBuildingName.Visible = true;
                
            }
        }

        private void frmClassroomInfo_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = cr.getCRMByCIDAndBName(cid, tName);

            //查询时的教学楼绑定教学楼名称
            cboxBuildingName.DataSource = cr.getTeachingBuilding();
            cboxBuildingName.DisplayMember = "TeachingBuildingName";
            cboxBuildingName.ValueMember = "TeachingBuildingName";
            //加载教师使用教室信息窗体时数据网格中显示的信息
            //dataGridView1.DataSource =cr.getAllClassroomInfos();
        }

        private void btnRetrive_Click(object sender, EventArgs e)
        {
            if (cboxQueryStyle.Text == "关键词搜索")
            {
                int cid = Convert.ToInt32(txtClassroomId.Text.Trim());
                dataGridView1.DataSource = cr.getClassroomStateByCID(cid);
                if (dataGridView1.RowCount == 0)
                    this.promot();
            }
            else if (cboxQueryStyle.Text == "目录搜索")
            {
                int cid = Convert.ToInt32(txtClassroomId.Text.Trim());
                string tbName = cboxBuildingName.Text.Trim();
                dataGridView1.DataSource = cr.getCRMByCIDAndBName(cid, tbName);
                if (dataGridView1.RowCount == 0)
                    this.promot();
            }
            else
            {
                MessageBox.Show("请输入完整的查询条件！");
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value == null)
            {
                return;
            }
            else
            {
                txtBuildingName.Text = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtRoomID.Text = dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtWeek.Text = dataGridView1[5, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtTName.Text = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtCourseName.Text = dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtPeriod.Text = dataGridView1[6, dataGridView1.CurrentCell.RowIndex].Value.ToString();
            }
        }

        private void tsbtnFirstItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                if (this.position != 0)
                {
                    foreach (DataGridViewRow dgvr in dataGridView1.Rows)
                    {
                        dgvr.Selected = false;
                    }
                    this.dataGridView1.Rows[0].Selected = true;
                    this.position = 0;
                }
            }
        }

        private void tsbtnUpItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                if (this.position > 0)
                {
                    foreach (DataGridViewRow dgvr in this.dataGridView1.Rows)
                    {
                        dgvr.Selected = false;
                    }
                    this.position--;
                    this.dataGridView1.Rows[this.position].Selected = true;
                    if (this.position == 0)
                    {
                        MessageBox.Show("当前已是第一条记录！");
                    }

                }
            }
        }

        private void tsbtnNextItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                if (this.position < this.dataGridView1.Rows.Count - 1)
                {
                    foreach (DataGridViewRow dgvr in this.dataGridView1.Rows)
                    {
                        dgvr.Selected = false;
                    }
                    this.position++;
                    this.dataGridView1.Rows[this.position].Selected = true;
                    if (this.position == this.dataGridView1.Rows.Count - 1)
                    {
                        MessageBox.Show("当前已是最后一条记录！");
                    }
                }
            }
        }

        private void tsbtnLastItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                if (this.position != this.dataGridView1.Rows.Count - 1)
                {
                    foreach (DataGridViewRow dgvr in this.dataGridView1.Rows)
                    {
                        dgvr.Selected = false;
                    }
                    this.position = this.dataGridView1.Rows.Count - 1;
                    this.dataGridView1.Rows[this.position].Selected = true;
                }
            }
        }
    }
}
